// pages/SCM/store/goodsAdd/goodsAdd.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

        data: {
                hideFlag: true,
                categories: [],
                imgArray: [],
                goodsItem: {},
                goodsList: [],
                goodsStatIds: [],
                accessType: 1,
                accessTypes: [{
                        id: 1,
                        name: '个人仓库',
                        checked: true
                }, {
                        id: 2,
                        name: '现场',
                        checked: false
                }, {
                        id: 3,
                        name: '购买',
                        checked: false
                }, {
                        id: 4,
                        name: '供应商',
                        checked: false
                }],
                uindex: 0,
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                this.setData({
                        warehouseId: options.warehouseId,
                        optaction: options.optaction,
                        warehouseName: options.warehouseName,
                        scrollHeight: app.getScrollHeight(0)
                });
                wx.setNavigationBarTitle({
                        title: options.warehouseName
                })
                this.getUsers();


        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {
                let that = this
                that.toast = that.selectComponent("#toast")
        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
                let that = this
                //添加物品
                if (this.data.addGoodsFlag) {
                        that.setData({
                                show: false
                        })
                        let list = this.data.selGoodsList
                        for (var i = 0; i < list.length; i++) {
                                this.selGoodsAdd(list[i]);
                        }
                        this.data.selGoodsList = [];
                        that.setData({
                                show: true
                        })
                }

        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },

        selGoodsAdd: function (selectGoods) {
                let index = this.data.goodsList.findIndex(item=>item.goodsId==selectGoods.goodsId);
                if (index > -1) {
                        this.data.goodsList[index].addNum = selectGoods.addNum;
                } else {
                        this.data.goodsList.push(selectGoods);
                }

                this.setData({
                        goodsList: this.data.goodsList
                });
                this.data.addGoodsFlag = false;
        },
        toChooseHospital: function () {
                wx.navigateTo({
                        url: '../../../chooseHospital/chooseHospital',
                })
        },
        toAddWarehouseGoods: function () {
                if (this.data.uindex == 0 && this.data.optaction == 2) {
                        wx.showToast({
                                title: '请先选择来源人',
                                image: '/images/gantan.png'
                        })
                        return;
                }
                let selGoodsList = [];
                this.data.goodsList.forEach(item=>{
                        selGoodsList.push({goodsId:item.goodsStatId,goodsName:item.goodsName,addNum:item.addNum})  
                })
                let params = '?warehouseId=' + this.data.warehouseId + '&optaction=' + this.data.optaction + '&warehouseName=' + this.data.warehouseName + '&selGoodsList=' + JSON.stringify(selGoodsList);
                if (this.data.uindex > 0 && this.data.accessType == 1) { //个人仓库
                        let uid = this.data.users[this.data.uindex].masterUid;
                        let uname = this.data.users[this.data.uindex].masterName;
                        params += '&uid=' + uid + '&uname=' + uname;
                }
                wx.navigateTo({
                        url: '../chooseGoods/chooseGoods' + params,
                })
        },
        inputName: function (e) {
                console.log('e====', e)
                let inputName = e.detail.value;
                let addNum = parseInt(inputName);
                let item = this.data.goodsList[e.currentTarget.dataset.index];
                if (addNum > item.goodsNum) {
                        wx.showToast({
                                title: '请小于库存数量',
                                image: '/images/gantan.png'
                        })
                        item.addNum = '';
                } else {
                        item.addNum = addNum;
                }
                this.setData({
                        goodsList: this.data.goodsList
                });

        },
        deleteGoods: function (e) {
                let index = e.currentTarget.dataset.index;
                this.data.goodsList.splice(index, 1);
                this.data.goodsStatIds.splice(index, 1);
                this.setData({
                        goodsList: this.data.goodsList
                });
        },
        getUsers: function () {
                let that = this
                lbbService.getStockUsers({}, function (res) {
                        let firstItem = {
                                'masterName': '无',
                                'masterUid': ''
                        };
                        res.data.unshift(firstItem);
                        let userNames = [];
                        res.data.forEach(item => {
                                userNames.push(item.masterName)
                        })
                        that.setData({
                                userNames: userNames,
                                users: res.data
                        })
                })
        },
        userChange: function (e) {
                console.log('picker发送选择改变，携带值为', e.detail.value)

                if (e.detail.value > 0 && this.data.optaction == '2') {
                        this.setData({
                                uindex: e.detail.value,
                                goodsList: [],
                                goodsStatIds: []
                        })
                } else {
                        this.setData({
                                uindex: e.detail.value
                        })
                }
                //
        },
        accessTypeChange: function (e) {
                let oldAccessType = this.data.accessType;
                let nowAccessType = e.detail.value;
                if (oldAccessType == 1 || nowAccessType == 1) {
                        this.setData({
                                accessType: e.detail.value,
                                goodsList: [],
                                goodsStatIds: []
                        })
                } else {
                        this.setData({
                                accessType: e.detail.value,
                        })
                }


        },

        formSubmit: function (e) {
                let that = this
                console.log(e)
                let formData = e.detail.value
                // that.setData({
                //         formData: formData
                // })
                // if (!formData.name) {
                //         wx.showToast({
                //                 title: '填写物品名称',
                //                 image: '/images/gantan.png'
                //         })
                //         return
                // }
                
                let title = '请选择' + (that.data.optaction == 2 ? '来源人' : '接收人');
                if (that.data.uindex == 0) {
                        wx.showToast({
                                title: title,
                                image: '/images/gantan.png'
                        })
                        return;
                }
                let goodsList = this.data.goodsList;
                if (goodsList.length == 0) {
                        wx.showToast({
                                title: '请添加物品',
                                image: '/images/gantan.png'
                        })
                        return;
                }
                let flag = true;
                let goodsDetails = [];
                goodsList.forEach(item => {
                        if (!item.addNum) {
                                flag = false;
                                that.toast.showToast(item.goodsName + '数量需大于零');
                                return false;
                        }
                        let goodsDetail = {};
                        goodsDetail.goodsId = item.goodsStatId;
                        goodsDetail.goodsNum = item.addNum;
                        goodsDetails.push(goodsDetail);
                });
                if (!flag) {
                        return;
                }
                if (this.data.uindex > 0) {
                        formData.uid = this.data.users[this.data.uindex].masterUid;
                }
                formData.detailJsonStr = JSON.stringify(goodsDetails);
                let changedLocationInfo = that.data.changedLocationInfo;
                if (changedLocationInfo && changedLocationInfo.hospitalId) {
                        formData.hospitalId = changedLocationInfo.hospitalId;
                }
                lbbService.accessGoods(formData, function (res) {
                        wx.showToast({
                                title: '提交成功',
                        })
                        setTimeout(function () {
                                wx.navigateBack({
                                        delta: 1
                                })
                        }, 1500)
                })

        },




})